package com.jymj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jymj.entity.CjEntity;
import com.jymj.entity.SysXzqEntity;
import com.jymj.entity.dto.ScorebarDto;
import com.jymj.entity.dto.ScorebarZhenDto;
import com.jymj.entity.landmanage.*;
import com.jymj.entity.vo.CompositeVo;
import com.jymj.entity.vo.ScoreVo;
import com.jymj.service.ScoreService;
import com.jymj.utils.PageUtils;
import com.jymj.utils.Query;
import com.jymj.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by ${lc} on 2020/12/11.
 */
@RequestMapping("/scorebar")
@RestController
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    /*
   * 查询村的扣分情况表
   * */
    @RequestMapping("/queryScorebar")
    public R queryScorebar(@RequestBody ScorebarDto scorebarDto){
        PageHelper.startPage(scorebarDto.getPage(),scorebarDto.getLimit());
        List<ScorebarEntity> scorebarEntities = scoreService.queryScorebar(scorebarDto);
        PageInfo<ScorebarEntity> scorebarEntityPageInfo = new PageInfo<>(scorebarEntities);
        return R.ok().put("data",scorebarEntityPageInfo);
    }

    /**
     * 查询扣分情况明细表
     * @param scorebarDto
     * @return
     */
    @PostMapping("/getScore")
    public R getScore(@RequestBody ScorebarDto scorebarDto){
        PageHelper.startPage(scorebarDto.getPage(),scorebarDto.getLimit());
        List<ScoreEntity> scoreEntities = scoreService.getScore(scorebarDto);
        PageInfo<ScoreEntity> scoreEntityPageInfo = new PageInfo<>(scoreEntities);
        return R.ok().put("data",scoreEntityPageInfo);
    }

    /**
     * 综合得分明细表
     * @param scorebarDto
     * @return
     */
    @PostMapping("/getZhscore")
    public R getZhscore(@RequestBody ScorebarDto scorebarDto){
        PageHelper.startPage(scorebarDto.getPage(),scorebarDto.getLimit());
        List<ScoreVo> scoreVos = scoreService.queryZhscore(scorebarDto);
        PageInfo<ScoreVo> scoreVoPageInfo = new PageInfo<>(scoreVos);
        return R.ok().put("data",scoreVoPageInfo);
    }

    /**
     * 计算公厕扣分
     * @param scorebarDto
     * @return
     */
    @PostMapping("/queryRecord")
    public R queryRecord(@RequestBody ScorebarDto scorebarDto){
        List<ToiletRecordEntity> toiletRecordEntities = scoreService.queryRecord(scorebarDto);
        return R.ok().put("data",toiletRecordEntities);
    }

    /**
     * 查询公厕扣分
     * @param scorebarDto
     * @return
     */
    @PostMapping("/getToiletRecord")
    public R getToiletRecord(@RequestBody ScorebarDto scorebarDto){
        PageHelper.startPage(scorebarDto.getPage(),scorebarDto.getLimit());
        List<PointRecordEntity> pointRecordEntities = scoreService.getToiletRecord(scorebarDto);
        PageInfo<PointRecordEntity> recordEntityPageInfo = new PageInfo<>(pointRecordEntities);
        return R.ok().put("data",recordEntityPageInfo);
    }

    /**
     * 区级人居环境综合得分情况统计表
     * @param date  yyyy-mm
     * @return
     */
    @PostMapping("/getZhdf")
    public R getZhdf(String date,Integer type){
        List<CompositeVo> compositeVos = scoreService.getZhdf(date,type);
        return R.ok().put("data",compositeVos);
    }



    /*
     * 查询通州各镇各村问题统计表
     */
    @RequestMapping("/queryZhenCode")
    public R queryZhenCode(@RequestBody ScorebarZhenDto scorebarZhenDto){
        String quCode = scorebarZhenDto.getCode();
        String date = scorebarZhenDto.getDate();
        List<ScorebarEntity> scorebarEntities = scoreService.queryZhenCode(quCode,date);
        Integer zfs = 0;
        Integer zwts = 0;
        for (ScorebarEntity scorebarEntity : scorebarEntities) {
            zfs += scorebarEntity.getScore();
            if (scorebarEntity.getScore() != 0){
                zwts += 1;
            }
        }
        BigDecimal avgf = BigDecimal.valueOf(zfs).divide(
                BigDecimal.valueOf(zwts),2, RoundingMode.HALF_UP);
        return R.ok().put("data",scorebarEntities).put("avg",avgf);
    }

    @RequestMapping("/queryZhenCodelist")
    public R queryZhenCodelist(@RequestBody ScorebarZhenDto scorebarZhenDto){
        String quCode = scorebarZhenDto.getCode();
        String date = scorebarZhenDto.getDate();
        List<ZhenInfo> zhenList = scoreService.queryZhenCodelist(quCode,date);
        return R.ok().put("data",zhenList);
    }

    /*
    * 按村查询近半年的分数和均分 参数 镇code
    * */
    @RequestMapping("/queryByMon")
    public R queryByMon(@RequestBody ScorebarDto scorebarDto){
        List<ScoreMonEntity> scoreMons = scoreService.queryByMon(scorebarDto);
        return R.ok().put("data",scoreMons);
    }

    /*
    * 环境监察排名表
    * */
    @RequestMapping("/queryRanking")
    public R queryRanking(@RequestBody Map<String,Object> params){
        List<ScoreZhenEntity> scoreZhenEntities = scoreService.queryRanking(params);
        return R.ok().put("data",scoreZhenEntities);
    }

    /*
     * 固定点位统计表
     */
    @RequestMapping("/queryGuding")
    public R queryGuding(){
        List<ZhenInfo> zhenList = scoreService.queryGuding();
        return R.ok().put("data",zhenList);
    }
    @RequestMapping("/saveScore")
    public R saveScore() {
        //获取当前村的code
        List<CjEntity> cjEntityList = scoreService.selectBycjCode();
        for (CjEntity cj : cjEntityList) {
            Map map = new HashMap();
            map.put("code", cj.getCode());
            map.put("xzqmc", cj.getXzqmc());
            updateScore(map);
        }
        return R.ok();
    }

    /*
     * 村庄打分
     **/
    @RequestMapping("/updateScore")
    public R updateScore(@RequestBody Map<String, Object> params) {
        CjEntity cjEntity = scoreService.selectCjobjByCode((String) params.get("code"));
        Integer type = params.get("type") == null ? 1 :(Integer) params.get("type");
        ScoreEntity scoreEntity = scoreService.scoreByCode(cjEntity,type);
        return R.ok().put("data",scoreEntity);
    }


    /*
     * 查询当前村的分
     * */
    @RequestMapping("/queryScoreCode")
    public R queryScoreCode(String code){
        CjEntity cjEntity = scoreService.selectCjobjByCode(code);
        ScorebarEntity scoreEntity = scoreService.scorebarByCode(cjEntity,2);
        return R.ok().put("data",scoreEntity);
    }

    @RequestMapping("/selectZhen")
    public R selectZhen( ){
        List<ZhenInfo> zhenList = scoreService.selectZhen();
        return R.ok().put("data",zhenList);
    }


}
